package com.dynamic.idea.quick.develop.plugin.action;

import com.dynamic.idea.quick.develop.plugin.dialog.QuickDevelopStartDialogWrapper;
import com.dynamic.idea.quick.develop.plugin.dialog.QuickDevelopStartDialogWrapper02;
import com.dynamic.idea.quick.develop.plugin.entity.base02.GeneralProjectStruct;
import com.dynamic.idea.quick.develop.plugin.entity.base02.GeneralRule;
import com.dynamic.idea.quick.develop.plugin.utils.GeneralRuleUtils;
import com.dynamic.idea.quick.develop.plugin.utils.ModulesUtils;
import com.dynamic.idea.quick.develop.plugin.utils.NotifyUtils;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;

/**
 * 启动按钮
 */
public class QuickDevelopStartAction extends AnAction {


    @Override
    public void actionPerformed(AnActionEvent e) {
//        NotificationGroup group = new NotificationGroup("bbb", NotificationDisplayType.BALLOON, true);
//        Notification notification = group.createNotification("我是一条提醒消息2", MessageType.INFO);
//        Notifications.Bus.notify(notification);

        Project project = e.getData(PlatformDataKeys.PROJECT);
        String basePath = project.getBasePath();
        System.out.println("basePath:"+basePath);

        String pomFile=basePath+"/pom.xml";
        Model mavenModel = ModulesUtils.getMavenModel(pomFile);

        String projectName = mavenModel.getName();
        //项目地址
        GeneralProjectStruct generalProjectStruct = GeneralRuleUtils.getProjectStructByName(projectName);
        generalProjectStruct.setRootPath(basePath);

        boolean configService = GeneralRuleUtils.isConfigService(projectName);

        // 未设置配置信息
        if (configService==false){
            NotifyUtils.notifyWarning("the "+projectName+" not set config info");
            return;
        }

        QuickDevelopStartDialogWrapper02 enumAutoGenerateDialogWrapper = new QuickDevelopStartDialogWrapper02(project);

        if (enumAutoGenerateDialogWrapper.showAndGet()) {
            // user pressed OK
        }


    }

    /**
     * 显示错误信息
     * @param errorMessage
     */
    private void notifyErrorMessage(String errorMessage){
        NotificationGroup group = new NotificationGroup("quickDevelopNotifyMavenEmptyERORR", NotificationDisplayType.BALLOON, true);
        Notification notification = group.createNotification(StringUtils.trimToEmpty(errorMessage), MessageType.ERROR);
        Notifications.Bus.notify(notification);
    }
}


